<?php
namespace Admin\Controller;
use Think\Controller;
class UserController extends CommntController{
    /**
     * 列表
     */
    public function index($keywords = '')
    {
        $map['username'] = array('like','%'.$keywords.'%');//搜索条件
    	$moder = M('admin');
        $count = $moder->count();
        $num   = 10;
        $Page  = new \Think\Page($count,$num);
    	$user = $moder->where($map)->field('id,username,email,update_at,status')->order('id DESC')->limit($Page->firstRow.','.$Page->listRows)->select();
        $show  = $Page->show();
        $this->assign('list',$user);
    	$this->assign('page',$show);
        $this->display();
    }
    /**
     * 添加
     */
     public function  user_add()
    {
    	$this->display();
    }
    /**
     * 添加
     */
    public function user_update()
    {
    	if (!IS_POST) {
    		echo "1";die;
    	}
    	$data['username']   = $_POST['username'];
    	$data['password']   = md5($_POST['password']);
    	$map['repassword']  = md5($_POST['repassword']);
    	$data['email']		= $_POST['email'];
    	$data['update_at']	= time();
    	$moder = M('admin');
    	
    	$user = $moder->where(array('username'=>$data['username']))->find();
    	if (isset($user)) {
    		echo "2";die;
    	}

    	if ($map['repassword'] != $data['password']) {
    		echo "3";die;
    	}
    	$email = $moder->where(array('email'=>$data['email']))->find();
    	if (isset($email)) {
    		echo "4";die;
    	}
        
    	if ($moder->add($data)) {
			$url = U('User/index');
			echo "5#".$url;die;
		}
    	
    }
    /**
     * 修改
     */
    public function user_edit()
    {
       $id   = $_GET['id'];
       $user = M('admin')->where(array('id'=>$id))->find();
       $this->assign('list',$user);
       $this->display();  
    }
    public function user_save()
    {
        // var_dump($_FILES);die;
        //判断是否是POST传过来的值
        if (!IS_POST) {
            echo "1";die;
        }
        //获取传过来的图片进行处理
        $file = $_FILES['imgs'];
        //修改的值进行赋值
        $id       = $_POST['id'];
        $data['username'] = $_POST['username'];
        $data['email']    = $_POST['email'];
        //判断file里是否有值并且判断图片名是否获取成功
        if (isset($file) && $file['name'] != '') {
            //判断上传文件是否是array里的类型
           $arrayName = array("image/jpg","image/png","image/jpeg","image/gif");
           $fiel_type = $file['type'];
           if (!in_array($fiel_type,$arrayName )) {
            //图片类型上传错误
               echo "2";die;
           }
        }
        //获取图片后缀并给图片重新命名
        $images = strchr($file['name'],'.');
        $imgtime= time('YmdHis');
        $typeimg= $imgtime.$images;
        //图片存放路径
        $path   = "./Public/admin/assets/img/user_code/".$typeimg;
        if (is_uploaded_file($file['tmp_name'])) { //判断指定$file是否是通过 HTTP POST 上传的
            if (move_uploaded_file($file['tmp_name'],$path)) { //将$file['tmp_name']上传的文件移动到$path新位置。
                $image = new \Think\Image();//图片处理类
                $image->open($path);
                // 按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg
                $image->thumb(150, 150,\Think\Image::IMAGE_THUMB_SCALE)->save($path);
                clearstatcache();//清除缓存
                if(filesize($path)>(50*1024))
                {
                    $image->thumb(400,400,\Think\Image::IMAGE_THUMB_SCALE)->save($path);
                }
                $data['img'] = $typeimg;
            }
        }      
        $user      = M('admin');
        $username = $user->where(array('username'=>$data['username']))->find();
        if (isset($username)) {
            //不能改成已经存在的用户名
            echo "3";die;
        }
        $user_name = $user->where(array('id'=>$id))->find();
        if ($user_name['img'] === 'avatar.jpg') {
            
        }else{
            $uploadDir = './Public/admin/assets/img/user_code/'.($user_name['img']);
            // 判断是id对应的字段否存在
            if ($user_name['img']) {
                unlink('./Public/admin/assets/img/user_code/'.$user_name['img']);
            }
        }
        $list = $user->where(array('id'=>$id))->save($data);
        if ( $list ) {
            //设置成功后跳转页面的地址，默认的返回页面是$_SERVER['HTTP_REFERER']
            // $this->success('修改成功', U('User/index'));
            $url = U('User/index');
            echo "4#".$url;die;
        }else{
            //错误页面的默认跳转页面是返回前一页，通常不需要设置
            // $this->error('修改失败');
            $url = U('User/user_edit');
            echo "5#".$url;die;
        }
    }
    /**
     * 修改状态禁用
     */
    public function user_status()
    {   
        $id = $_GET['id'];
        $moder = M('admin');
        $user = $moder->where(array('id'=>$id))->find();
        if($user['status'] == 1){
            $user['status'] = 0;
            $moder->save($user);
            echo "1";die;
        }else if($user['status'] == 0){
            $user['status'] = 1;
             $moder->save($user);
             echo "2";die;
        }
    }
    /**
     * 删除
     */
    public function user_delete()
    {
        $id = $_GET['id'];
        $user = M('admin');
        if ($user->where(array('id'=>$id))->delete()) {
           echo "1";die;
        }else{
           echo "2";die;
        }
    }
}